<template>
  <div id="customer-department-detail">
    <nav aria-label="breadcrumb" role="navigation">
      <ol class="breadcrumb pb-0">
        <li class="breadcrumb-item">
          <router-link to="/customers">企业客户</router-link>
        </li>
        <li class="breadcrumb-item">
          <router-link to="/customer/departments">部门</router-link>
        </li>
        <li class="breadcrumb-item active" aria-current="page">详情</li>

        <li class="breadcrumb-item ms-auto">
          <router-link to="/passengers">员工信息</router-link>
        </li>
      </ol>
    </nav>
    <div class="card">
      <div class="card-body">
        <span class="text-warning">注意：</span
        >只有部门经理或部门审批人可以在前台系统中查看部门报表。
      </div>
      <table class="table table-striped table-sm">
        <thead>
          <tr>
            <th>公司</th>
            <th>部门编号</th>
            <th>部门名称</th>
            <th>上级部门</th>
            <th>备注</th>
            <th></th>
            <th></th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <template v-if="detail !== null">
            <tr>
              <td>{{ detail.customerName }}</td>
              <td>{{ detail.departmentNo }}</td>
              <td>{{ detail.name }}</td>
              <td>{{ detail.parentDepartmentName }}</td>
              <td>{{ detail.remark }}</td>
              <td>
                <router-link
                  :to="`/customer/department?id=` + detail.id"
                  class="btn btn-sm btn-primary text-white"
                  >修改</router-link
                >
              </td>
              <td>
                <router-link
                  :to="`/customer/department?id=` + detail.id + `&copies=1`"
                  class="ms-3"
                  >复制</router-link
                >
              </td>
              <td>
                <a
                  href="javascript:void(0)"
                  @click.stop="addApprover()"
                  class="btn btn-success ms-3"
                  >设置部门经理或审批人</a
                >
              </td>
            </tr>
          </template>
        </tbody>
      </table>
      <template v-if="detail !== null && detail.approvers.length > 0">
        <table class="table table-sm bg-light">
          <caption>
            部门经理及审批人
          </caption>
          <tr>
            <th>用户名</th>
            <th>姓名</th>
            <th>部门</th>
            <th>角色</th>
            <th>手机</th>
            <th>电子邮件</th>
            <td></td>
          </tr>
          <tr v-for="(ta, index) of detail.approvers" :key="index">
            <td>{{ ta.username }}</td>
            <td>{{ ta.fullName }}</td>
            <td>{{ ta.departmentName }}</td>
            <td>{{ showRoleType(ta.roleType) }}</td>
            <td>{{ ta.mobile }}</td>
            <td>{{ ta.email }}</td>
            <td>
              <template v-if="isDataAdmin">
                <button
                  class="btn btn-sm btn-warning"
                  @click.stop="deleteApprover(ta.id)"
                >
                  移除审批人
                </button>
              </template>
            </td>
          </tr>
        </table>
      </template>
    </div>

    <my-customer-approver ref="customerApproverModal"></my-customer-approver>
  </div>
</template>

<script>
import {
  searchDepartments,
  searchCustomers,
  deleteCustomerApprover,
  searchDepartment,
} from "@/api/customer.js";
import MySelectCustomer from "@/components/my-select2-customer.vue";
import MyCustomerApprover from "@/components/modal/my-modal-customer-approver.vue";

export default {
  components: {
    MySelectCustomer,
    MyCustomerApprover,
  },
  data() {
    return {
      id: 0,
      customerId: 0,
      deptId: 0,
      name: "",
      parentDeptId: 0,
      mobile: "",
      phone: "",
      linkMan: "",
      email: "",
      remark: "",

      dataList: [],
      detail: null,

      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 15,
        pageTotal: 0,
      },
      searchName: "",
      searchCustomerId: -1,
    };
  },
  computed: {
    isDataAdmin() {
      return this.$store.getters.isDataAdmin;
    },
  },
  mounted: function () {
    this.id = parseInt(this.$route.params.id);
    this.search();
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showLoading: function (msg) {
      // this.$store.dispatch('showLoading', { 'loadingText': msg })
    },
    hideLoading: function () {
      // this.$store.dispatch('hideLoading')
    },
    search: function () {
      searchDepartment(this.id, (v) => {
        this.detail = v;
        console.log(this.detail);
      });
    },

    reset: function () {
      this.name = "";
      this.searchName = "";
      this.searchCustomerId = -1;
      this.sc.pageNo = 1;
      this.search();
    },
    addApprover: function () {
      const info = this.detail;

      this.$refs.customerApproverModal
        .modal(info)
        .then((v) => {
          console.log(v);
          if (v.status === "OK") {
            this.search();
          }
        })
        .catch((ex) => {});
    },
    deleteApprover: function (id) {
      deleteCustomerApprover(id, (v) => {
        if (v.status === "OK") {
          this.showErrMsg("操作成功");
          this.search();
        } else {
          this.showErrMsg(v.errMsg, "danger");
        }
      });
    },
    showRoleType: function (val) {
      switch (val) {
        case 10:
          return "经理";
        case 20:
          return "审批人";
        default:
          return val;
      }
    },
  },
};
</script>
